//
//  GoodsdetViewController.m
//  IGOtest
//
//  Created by administrator on 14-2-21.
//  Copyright (c) 2014年 administrator. All rights reserved.
//

#import "GoodsdetViewController.h"
#import "igoods.h"
#import "ShopingCartViewController.h"
#import "commentViewController.h"
#import "AppDelegate.h"
#import "LoginViewController.h"

@interface GoodsdetViewController ()<UIAlertViewDelegate>
{
    AppDelegate *del;
    
}


@end

@implementation GoodsdetViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    del =[[UIApplication sharedApplication]delegate ];
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 50) ];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn setTitle:@"< Back" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(130, 0, 80, 50)];
    label.textColor = [UIColor greenColor];
    label.text = @"商品详情";
    [self.view addSubview:label];
    self.pinmingtxt.text = _goods.igoodsname;
    self.pricetxt.text = _goods.igoodsprice;
    self.leixingtxt.text = _goods.igoodscomment;
    self.igoodsimage.image = _goods.igoodsimage;
    self.allprice.text = _goods.igoodsprice;
    
    self.amount.text = [NSString stringWithFormat:@"%d",(int)self.mystepper.value];
//    UIButton *button1 = [[UIButton alloc]init ];
//    [uibutton];
//    [self.view addSubview:button1];
    
    
}

-(void)btnclick:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)Nowmaibtn:(UIButton *)sender {
    if (del.username ==nil) {
        LoginViewController *login =[[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil ];
        [self presentViewController:login animated:YES completion:nil];
    }
    else{
        commentViewController *comment = [[commentViewController alloc]initWithNibName:@"commentViewController" bundle:nil ];
        [self presentViewController:comment animated:YES completion:nil];

    }
    
}

- (IBAction)jiarubtn:(UIButton *)sender
{
    if (del.username == nil) {
        LoginViewController *login =[[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil ];
        [self presentViewController:login animated:YES completion:nil];
    }
    else
    {
        AppDelegate *del = [[UIApplication sharedApplication]delegate];
        [del.cartList addObject:self.goods];
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"加入购物车成功" delegate:self cancelButtonTitle:@"留在该页面" otherButtonTitles:@"前往购物车", nil ];
        [alter show];

        
    }
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{
    if (buttonIndex == 1) {
        
        NSURL *url = [NSURL URLWithString:@"http://10.207.1.23:8888/user/addorderdetails.php"];
        
        //根据NSURL创建请求
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
        NSError *error = nil;
        NSURLResponse *response = nil;
        
        //设置请求方式
        [request setHTTPMethod:@"POST"];
        
        //定义参数
        //NSString *str = @"username=10000&password=2&email=3";
        float c =  [self.allprice.text floatValue];
        int gooid = 1;
        NSString *st=@"a";
        NSString *str = [NSString stringWithFormat:@"ordernumber=%@&goodsid=%d&count=%d&unitprice=%f",st,gooid,[self.amount.text intValue],c];
        NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
        
        //发送body
        [request setHTTPBody:data];
        
        //发送同步请求，data即为返回的数据
        [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        ShopingCartViewController *shop = [[ShopingCartViewController alloc]initWithNibName:@"ShopingCartViewController" bundle:nil ];
        //shop.goodss = self.goods;
        shop.allprices = self.allprice.text;
        shop.amounts = self.amount.text;
        
        [self presentViewController:shop animated:YES completion:nil];
    }

 
}
- (IBAction)closeKeyBoard:(id)sender {
    [self.view endEditing:YES];
}
- (IBAction)myStepperChange:(id)sender {
    self.amount.text = [NSString stringWithFormat:@"%d",(int)self.mystepper.value];
    int a = [self.pricetxt.text intValue];
    
    int b = [self.amount.text intValue];
    
    self.allprice.text = [NSString stringWithFormat:@"%d",a*b];
    
}

- (IBAction)changeAmount:(id)sender {
    int a = [self.pricetxt.text intValue];
    
    int b = [self.amount.text intValue];
    
    self.allprice.text = [NSString stringWithFormat:@"%d",a*b];
    
}
@end
